// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// c [author] rhyme_lph
// E [email]  rhymelph@gmail.com
// h [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/mixin/check_plan_mixin.dart';
import 'package:finance_app/src/provider/dashboard/purchase/views/purchases_bill_provider.dart';
import 'package:finance_app/src/provider/dashboard/purchase/views/purchases_expense_provider.dart';
import 'package:finance_app/src/provider/dashboard/purchase/views/purchases_po_provider.dart';
import 'package:finance_app/src/provider/dashboard/purchase/views/purchases_summary_provider.dart';
import 'package:finance_app/src/router_gen/router_gen_providers.dart';
import 'package:finance_app/src/utils/permission_utils.dart';
import 'package:finance_app/src/widgets/bottom_sheet/dashboard_add_bottom_sheet.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';

class PurchasesProvider extends BaseProvider with CheckPlanMixin {
  List<String> get tabList => [
        S.current.summary,
        if (Config.hasPermission(PermissionUtils.purchaseOrder)) S.current.po,
        if (Config.hasPermission(PermissionUtils.bill)) S.current.bill,
        if (Config.hasPermission(PermissionUtils.expense)) S.current.expense,
      ];

  bool buttonBarIsUp = true;

  @override
  Future get dataFuture => null;

  @override
  List<BaseProvider> registerChildProviders() {
    return [
      PurchasesSummaryProvider(),
      PurchasesPoProvider(),
      PurchasesBillProvider(),
      PurchasesExpenseProvider(),
    ];
  }

  void handleScrollUpdate(ScrollController controller) {
    if (controller.position.userScrollDirection == ScrollDirection.reverse &&
        buttonBarIsUp == true) {
      //向下滑动
      buttonBarIsUp = false;
      update();
    }
    if (controller.position.userScrollDirection == ScrollDirection.forward &&
        buttonBarIsUp == false) {
      buttonBarIsUp = true;
      update();
    }
  }

  void onNavAdd() {
    if (isPlanExpired()) return;
    obtainContext?.call((context) async {
      if (tabController != null) {
        currentIndex =
            (tabController.index + tabController.offset).round().clamp(0, 100);
      }
      if (tabList[currentIndex] == S.current.summary) {
        await showDashboardAddBottomSheet(context,
            dashTypes: [DashBoardAddType.purchaseSummary]);
        getChildProvider<PurchasesSummaryProvider>()?.onRefresh();
      } else if (tabList[currentIndex] == S.current.po) {
        Routes.navigateTo(RouterGenProviders.newPurchaseOrderPage);
        getChildProvider<PurchasesPoProvider>()?.onRefresh();
      } else if (tabList[currentIndex] == S.current.bill) {
        await Routes.navigateTo(RouterGenProviders.newBillPage);
        getChildProvider<PurchasesBillProvider>()?.onRefresh();
      } else if (tabList[currentIndex] == S.current.expense) {
        await Routes.navigateTo(RouterGenProviders.newExpensePage);
        getChildProvider<PurchasesExpenseProvider>()?.onRefresh();
      }
    });
  }

  int currentIndex = 0;

  TabController tabController;

  void onChangeTab(TabController tabController) {
    currentIndex = (tabController.index + tabController.offset).round();
  }
}
